package com.njust.controller;

import com.njust.service.GearService;
import com.njust.service.MotorService;
import com.njust.service.TrainService;
import com.njust.utils.DateUtil;
import com.njust.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.Date;

/**
 * 数据报表
 * @author zjwpro@foxmail.com
 * @date 2018/1/9 下午10:08
 */
@RestController
@RequestMapping("/table")
@Api("数据报表")
public class TableController {

    @Autowired
    private MotorService motorService;

    @Autowired
    private TrainService trainService;

    @Autowired
    private GearService gearService;


    /**
     * @param pre 开始时间0点 到24点的过车情况
     * @param pagenumber
     * @param pagesize
     * @return
     */
    @GetMapping("/report/{pre}/{pagenumber}/{pagesize}")
    @ApiOperation(value="行车报表列车综合数据")
    public ResultVO findByTrainDate(@PathVariable("pre")String pre, @PathVariable(name = "pagenumber")Integer pagenumber,
                                    @PathVariable(name ="pagesize")Integer pagesize) throws ParseException {
        //将Long类型的时间转为Date类型的数据
        //String pre1="20171016000000";
        //原来的为pre1
        Date receivetime = DateUtil.String2Date(pre);
        //此处也进行了修改，将行车报表的数据查的范围改为5:00-5:00
        long time1 =  receivetime.getTime()+12600000;
        Date firsttime = new Date (time1);
        long time=firsttime.getTime()+86400000;
        Date lasttime=new Date(time);
        return trainService.findByTrainDate(firsttime,lasttime,pagenumber,pagesize);
    }

    /**
     * 返回具体电机综合数据
     * @param trainOnlyid
     * @return
     */
    @GetMapping("/{trainOnlyid}/{trainDirection}/motordetail")
    @ApiOperation(value="电机综合数据")
    public ResultVO motorDetail(@PathVariable(name = "trainOnlyid")Long trainOnlyid,
                                @PathVariable("trainDirection")Integer trainDirection){
        return motorService.findByTrainOnlyid(trainOnlyid,trainDirection);
    }

    /**
     * 前端 列车记录编号和电机号 返回具体的数据
     * @param trainOnlyid
     * @param motorNum
     * @return
     */
    @GetMapping("/{trainOnlyid}/{trainDirection}/{motorNum}/geardetail")
    @ApiOperation(value="齿综合数据")
    public ResultVO gearDetail(@PathVariable(name = "trainOnlyid")Long trainOnlyid,
                               @PathVariable(name = "motorNum")Integer motorNum,
                               @PathVariable("trainDirection")Integer trainDirection){
        return gearService.findByTrainOnlyidAndMotorId(trainOnlyid,motorNum,trainDirection);
    }

    /**
     * 返回左右激光原始数据
     * @param trainOnlyid 列车记录编号
     * @param motorNum 电机编号（1-8）
     * @return
     */
    @GetMapping("/{trainOnlyid}/{trainDirection}/{motorNum}/laser")
    @ApiOperation(value="激光原始数据" )
    public ResultVO laserData(@PathVariable(name = "trainOnlyid")Long trainOnlyid,
                              @PathVariable(name = "motorNum") Integer motorNum,
                              @PathVariable("trainDirection")Integer trainDirection){
        return motorService.findByTrainOnlyidAndMotorNum(trainOnlyid,motorNum,trainDirection);
    }
}
